package com.pb.infra.taskManage.service;

import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.ExecuteScriptUtil;
import com.pb.infra.taskManage.entity.MaJobVmCfg;
import com.pb.infra.taskManage.mapper.MaJobVmCfgMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 任务调度引擎 虚拟机配置表(MaJobVmCfg)业务层
 *
 * @author 贾轶飞
 * @since 2022-10-17 15:23:55
 */
@Slf4j
@Service("maJobVmCfgService")
public class MaJobVmCfgService {

    @Value("${vm.path}")
    private String vmPath;

    @Resource
    private MaJobVmCfgMapper maJobVmCfgMapper;


    /**
     * 查询
     *
     * @param maJobVmCfg 查询参数
     * @return 集合
     */
    public Result queryList(MaJobVmCfg maJobVmCfg) {

        return Result.ok(this.maJobVmCfgMapper.queryList(maJobVmCfg));
    }

    /**
     * 添加
     *
     * @param maJobVmCfg 参数
     * @return 受影响条数
     */
    public Result insert(MaJobVmCfg maJobVmCfg) {
        return Result.ok(this.maJobVmCfgMapper.insert(maJobVmCfg));
    }

    /**
     * 修改
     *
     * @param maJobVmCfg 参数
     * @return 受影响条数
     */
    public Result update(MaJobVmCfg maJobVmCfg) {
        return Result.ok(this.maJobVmCfgMapper.update(maJobVmCfg));
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return 受影响条数
     */
    public Result delete(String id) {
        return Result.ok(this.maJobVmCfgMapper.deleteById(id) > 0);
    }

    @Transactional
    public Result etlMaJobTab(MaJobVmCfg maJobVmCfg) {

        //获取切换的虚拟机配置IP数据
        List<String> resRestStatus = new ArrayList<>();
        MaJobVmCfg selOnVmCfg=new MaJobVmCfg();
        selOnVmCfg.setVmStat("1");
        MaJobVmCfg maJobVmCfgs = this.maJobVmCfgMapper.selOne(selOnVmCfg);
        if (maJobVmCfgs != null) {
            String rootPath = "";
            try {
                rootPath = new ClassPathResource("").getURL().getPath();
            } catch (IOException e) {
                e.printStackTrace();
                return Result.ok("获取可执行脚本根路径异常：" + e.getMessage());
            }
            String scriptPath = rootPath + vmPath;

            MaJobVmCfg endVm = new MaJobVmCfg();
//            endVm.setVmStat("0");
            this.maJobVmCfgMapper.queryList(endVm).forEach(item -> {
                //远程结束调度任务进程
                List<String> endTaskList = new ArrayList<>();
                endTaskList.add("sh");
                endTaskList.add(scriptPath+"endTaskJob.sh");
                endTaskList.add(item.getVmIp());
                resRestStatus.add(ExecuteScriptUtil.runJobCmd(endTaskList, true) + "");
                resRestStatus.add("结束" + item.getVmIp() + "中已存在的任务进程");
            });

            //将所有使用的状态改为 未使用
            maJobVmCfgMapper.updateEnd();
            //将选中的ID改为 使用状态
            MaJobVmCfg cfg1 = new MaJobVmCfg();
            cfg1.setId(maJobVmCfg.getId());
            cfg1.setVmStat("1");
            cfg1.setVmParam1(maJobVmCfg.getVmParam1());
            maJobVmCfgMapper.update(cfg1);

            //启动的任务
            //远程结束 批量执行 任务  启动
            maJobVmCfg.getTaskList().forEach(item -> {
                MaJobVmCfg startTask = this.maJobVmCfgMapper.selOne(selOnVmCfg);
                List<String> monTaskList = new ArrayList<>();
                monTaskList.add("sh");
                monTaskList.add(scriptPath+"startMon.sh");
                monTaskList.add(startTask.getVmIp());
                monTaskList.add(item.get("SYS_NO"));
                monTaskList.add(startTask.getVmParam1());
                resRestStatus.add(ExecuteScriptUtil.runJobCmd(monTaskList, true) + "");

                List<String> startTaskList = new ArrayList<>();
                startTaskList.add("sh");
                startTaskList.add(scriptPath+"startTaskJob.sh");
                startTaskList.add(startTask.getVmIp());
                startTaskList.add(item.get("SYS_NO"));
                resRestStatus.add("结束" + item.get("SYS_NO") + "任务进程");
                resRestStatus.add(ExecuteScriptUtil.runJobCmd(startTaskList, true) + "");
            });

//#ssh ${IP} source "/home/ftpapp/.bash_profile;nohup /home/ftpapp/bin/ma_jobmain -s ${SYS_NO} &" > nohup.out &


        }

        return Result.ok(resRestStatus);
    }
}
